using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using SBPweb.Themes.Windows.Skins;
using SBPweb.Themes.Windows;

namespace SBPweb.Controls.Windows.Bar
{
	/// <summary>
	/// Class of Bar
	/// </summary>
	[ToolboxBitmap(typeof(System.Windows.Forms.ToolStrip))]
	public partial class Bar : ToolStrip
	{
		#region Private members

		private bool mOpenedCustomization;
		private Orientation mOrientation;
		private bool mFullSize = false;
		private bool mShowCustomization = false;
		private DockStyle mDockedTo = DockStyle.Top;

		#endregion

		#region Constructor

		public Bar()
		{
			InitializeComponent();
			RegisterEventHandlers();
		}

		private void RegisterEventHandlers()
		{
			this.Padding = new Padding(this.Padding.Left + 2, this.Padding.Top + 2, this.Padding.Right + 2, this.Padding.Bottom + 2);
			this.ParentChanged += new EventHandler(Bar_ParentChanged);
			this.BeginDrag += new EventHandler(Bar_BeginDrag);
			this.EndDrag += new EventHandler(Bar_EndDrag);
			this.MouseClick += new MouseEventHandler(Bar_MouseClick);
			this.MouseDown += new MouseEventHandler(Bar_MouseDown);
			this.MouseMove += new MouseEventHandler(Bar_MouseMove);
			this.MouseLeave += new EventHandler(Bar_MouseLeave);
			this.LocationChanged += new EventHandler(Bar_LocationChanged);
			this.ItemAdded += new ToolStripItemEventHandler(Bar_ItemAdded);
			this.ItemRemoved += new ToolStripItemEventHandler(Bar_ItemRemoved);
			this.ParentChanged += new EventHandler(Bar_ParentChanged);
			this.DockChanged += new EventHandler(Bar_DockChanged);
			DefaultLookAndFeel.OnSkinChanged += new EventHandler(DefaultLookAndFeel_OnSkinChanged);
		}

		#endregion

		#region Properties
		
		public DockStyle DockedTo
		{
			get 
			{ 
				return mDockedTo; 
			}
		}

		public bool OpenedCustomization
		{
			get
			{
				return mOpenedCustomization;
			}
		}

		public bool FullSize
		{
			get
			{
				return mFullSize && !AutoSize;
			}
			set
			{
				mFullSize = value;
				if (FullSize)
				{
					ResizeOnFullSize();
				}
				Invalidate();
			}
		}

		public bool ShowCustomization
		{
			get
			{
				return mShowCustomization;
			}
			set
			{
				mShowCustomization = value;
			}
		}

		#endregion

		#region Orientation changing

		protected override void OnInvalidated(InvalidateEventArgs e)
		{
			if (this.Orientation != mOrientation)
			{
				mOrientation = this.Orientation;
				OrientationChanged();
			}
			base.OnInvalidated(e);
		}

		private void OrientationChanged()
		{
			if (!this.AutoSize)
			{
				System.Drawing.Size minSize = new System.Drawing.Size(this.MinimumSize.Height, this.MinimumSize.Width);
				System.Drawing.Size maxSize = new System.Drawing.Size(this.MaximumSize.Height, this.MaximumSize.Width);
				this.MinimumSize = new System.Drawing.Size(0, 0);
				this.MaximumSize = new System.Drawing.Size(0, 0);
				this.Size = new System.Drawing.Size(this.Size.Height, this.Size.Width);
				this.MinimumSize = minSize;
				this.MaximumSize = maxSize;
				if (this.Orientation == Orientation.Horizontal)
				{
					this.ImageScalingSize = new System.Drawing.Size(this.Height - 9, this.Height - 9);
				}
				else
				{
					this.ImageScalingSize = new System.Drawing.Size(this.Width - 9, this.Width - 9);
				}
				this.Invalidate();
				ResizeOnFullSize();
			}
		}

		#endregion

		private void ResizeOnFullSize()
		{
			if (!FullSize || Parent == null)
			{
				return;
			}

			if (this.Orientation == Orientation.Horizontal)
			{
				int height = 0;
				foreach (ToolStripItem tsi in this.Items)
				{
					if (tsi.Width > tsi.Height)
					{
						tsi.Height = tsi.Width;
					}
					int itemHeight = tsi.AutoSize && tsi.Image != null ? tsi.Image.Height + tsi.Padding.Vertical : tsi.Height;
					if (itemHeight > height)
					{
						height = itemHeight;
					}
				}
				int newHeight = height + this.Padding.Vertical;
				this.Height = newHeight;
				this.Width = this.Parent.Width;
			}
			else
			{
				int width = 0;
				foreach (ToolStripItem tsi in this.Items)
				{
					if (tsi.Height > tsi.Width)
					{
						tsi.Width = tsi.Height;
					}
					int itemWidth = tsi.AutoSize && tsi.Image != null ? tsi.Image.Width + tsi.Padding.Horizontal : tsi.Width;
					if (itemWidth > width)
					{
						width = itemWidth;
					}
				}
				int newWidth = width + this.Padding.Horizontal;
				this.Width = newWidth;
				this.Height = this.Parent.Height;
			}
			this.Invalidate(true);
		}

		#region Events

		Control parent;
		Control parentParent;

		void Bar_BeginDrag(object sender, EventArgs e)
		{
			parent = this.Parent;
			parentParent = this.Parent.Parent;
		}

		void Bar_EndDrag(object sender, EventArgs e)
		{
			if (this.Parent.Parent != parentParent)
			{
				(parent as ToolStripPanel).Controls.Add(this);
				this.Show();
			}
		}

		void Bar_ItemRemoved(object sender, ToolStripItemEventArgs e)
		{
			e.Item.MouseMove -= new MouseEventHandler(Item_MouseMove);
			e.Item.MouseLeave -= new EventHandler(Item_MouseLeave);
			e.Item.MouseDown -= new MouseEventHandler(Item_MouseDown);
			e.Item.MouseUp -= new MouseEventHandler(Item_MouseUp);
			this.MinimumSize = this.Size;
		}

		void Bar_ItemAdded(object sender, ToolStripItemEventArgs e)
		{
			e.Item.MouseMove += new MouseEventHandler(Item_MouseMove);
			e.Item.MouseLeave += new EventHandler(Item_MouseLeave);
			e.Item.MouseDown += new MouseEventHandler(Item_MouseDown);
			e.Item.MouseUp += new MouseEventHandler(Item_MouseUp);
			e.Item.Padding = new Padding(3);
			this.MinimumSize = this.Size;
		}

		void Item_MouseUp(object sender, MouseEventArgs e)
		{
			if (sender is ToolStripButton)
			{
				this.Invalidate(true);
				(sender as ToolStripButton).Invalidate();
			}
		}

		void Item_MouseDown(object sender, MouseEventArgs e)
		{
			if (sender is ToolStripButton)
			{
				this.Invalidate(true);
				(sender as ToolStripButton).Invalidate();
			}
		}

		void Item_MouseLeave(object sender, EventArgs e)
		{
			if (sender is ToolStripButton)
			{
				this.Invalidate(true);
				(sender as ToolStripButton).Invalidate();
			}
		}

		void Item_MouseMove(object sender, MouseEventArgs e)
		{
			if (sender is ToolStripButton)
			{
				this.Invalidate(true);
				(sender as ToolStripButton).Invalidate();
			}
		}

		void Bar_LocationChanged(object sender, EventArgs e)
		{
			if (FullSize)
			{
				this.Invalidate(true);
				ResizeOnFullSize();
			}
			else
			{
				this.Invalidate(true);
			}
		}

		void Bar_MouseLeave(object sender, EventArgs e)
		{
			this.Invalidate(true);
		}

		void Bar_MouseMove(object sender, MouseEventArgs e)
		{
			this.Invalidate(true);
		}

		void Bar_MouseDown(object sender, MouseEventArgs e)
		{
			this.Invalidate(true);
		}

		void Bar_MouseClick(object sender, MouseEventArgs e)
		{
			if (!ShowCustomization)
			{
				return;
			}

			if ((Orientation == Orientation.Vertical && e.X > 0 && e.X < this.Width && e.Y > this.Height - 15 && e.Y < this.Height)
				||
				(Orientation == Orientation.Horizontal && e.X > this.Width - 15 && e.X < this.Width && e.Y > 0 && e.Y < this.Height))
			{
				mOpenedCustomization = !mOpenedCustomization;
			}
		}

		void Bar_ParentChanged(object sender, EventArgs e)
		{
			if (this.Parent != null)
				this.Parent.Resize += new EventHandler(Parent_Resize);

			if (this.Parent != null)
				mDockedTo = this.Parent.Dock;
		}

		void Parent_Resize(object sender, EventArgs e)
		{
			ResizeOnFullSize();
		}

		void Bar_DockChanged(object sender, EventArgs e)
		{
			if (this.Parent != null)
			{
				this.Parent.Invalidate(true);
			}
		}

		void DefaultLookAndFeel_OnSkinChanged(object sender, EventArgs e)
		{
			ResizeOnFullSize();
		}

		#endregion
	}
}
